//
//  MyModule.m
//  MisnewOperating
//
//  Created by lijunfeng on 2019/3/13.
//  Copyright © 2019年 Facebook. All rights reserved.
//
#import "MyModule.h"
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
 
#define WeakSelf __weak typeof(self) weakSelf = self;

//是否是空对象
#define MNIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))
// 字符串为空
#define MNStringIsEmpty(__string)           ((__string.length == 0) || MNIsEmpty(__string))
// 字符串不为空
#define MNStringIsNotEmpty(__string)        (!MNStringIsEmpty(__string))

static  NSString* const RCTTypegetCodeKey = @"getCodeKey";
static  NSString* const RCTTypegetWXCodeKey = @"wechat";
static  NSString* const RCTTypegetAliCodeKey = @"alipay";


static  NSString* const RCTTypeRegisterId = @"getRegisterId";//获取极光注册id
static  NSString* const wechatPagetUrl = @"pages/index/index";


@interface MyModule()<RCTBridgeModule,UIAlertViewDelegate>
//@property (nonatomic, copy)RCTPromiseResolveBlock resolveBlock;
//@property (nonatomic, copy)RCTPromiseRejectBlock rejectBlock;
@end


@implementation MyModule
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(initSchemeToJS:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){

}

RCT_EXPORT_METHOD(handleMessage:(NSDictionary *)msg resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
  dispatch_async(dispatch_get_main_queue(), ^{
    NSString *RCTTypeString = [msg objectForKey:@"type"];

    if ([RCTTypeString isEqualToString:RCTTypegetCodeKey]){
      if (resolve) {
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString *deploymentKey = [infoDictionary objectForKey:@"CodePushDeploymentKey"];
        resolve(deploymentKey);
      }
    }
  });
}

- (void)onBack:(id)sender {
  AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  
}

- (UIImage*)createImageWithColor: (UIColor*) color
{
  CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  UIGraphicsBeginImageContext(rect.size);
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetFillColorWithColor(context, [color CGColor]);
  CGContextFillRect(context, rect);
  UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return theImage;
}




- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (buttonIndex == 1) {
      NSString *url = @"itms://itunes.apple.com/cn/app/支付宝-让生活更简单/id333206289?mt=8";
      url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  }
}

+ (id)allocWithZone:(struct _NSZone *)zone {
  static MyModule *sharedInstance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedInstance = [super allocWithZone:zone];
  });
  return sharedInstance;
}

- (instancetype)init {
  self = [super init];
  if (self) {
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter removeObserver:self];
    [defaultCenter addObserver:self
                      selector:@selector(sendCustomEvent:)
                          name:@"sendCustomEventNotification"
                        object:nil];
  }
  return self;
}
+ (BOOL)requiresMainQueueSetup
{
  return NO;
}
- (NSArray<NSString *> *)supportedEvents
{
  return @[@"schemeListener"];
}

/// 接收通知的方法，接收到通知后发送事件到RN端。RN端接收到事件后可以进行相应的逻辑处理或界面跳转
- (void)sendCustomEvent:(NSNotification *)notification {

  [self sendEventWithName:@"schemeListener" body:notification.object];
}

 

@end
